package com.zhida.smartcontrol.dialog.tdkgdialog;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jay.tools.recycler.OnItemClick;
import com.jay.tools.recycler.decoration.RecyclerViewItemDecoration;
import com.jay.tools.utils.DimenUtils;
import com.jay.tools.utils.ResUtil;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/12/3 003 14:47
 * description: 通道开关
 */
public class TdkgDialogFragment extends MVPBaseDialogFragment<TdkgDialogContract.View, TdkgDialogPresenter> implements TdkgDialogContract.View {
    @BindView(R.id.recycler_view)
    RecyclerView mRView;

    private String mSwitchType;
    private List<LampBean> mLampList;
    private TdkgAdapter mAdapter;
    private String mType="0";

    public static TdkgDialogFragment newInstance(String switchType, List<LampBean> data) {
        Bundle args = new Bundle();
        args.putString("type", switchType);
        args.putParcelableArrayList("list", (ArrayList<? extends Parcelable>) data);
        TdkgDialogFragment fragment = new TdkgDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.dialog_tdkg;
    }

    @Override
    protected void initDatas(Bundle savedInstanceState) {
        mRView.setLayoutManager(new LinearLayoutManager(mActivity));
        mRView.addItemDecoration(new RecyclerViewItemDecoration.Builder(mActivity)
                .thickness(DimenUtils.dp2px(mActivity, 1))
                .color(ResUtil.getColor(mActivity, R.color.c_EEEEEE))
                .create());

        mAdapter = new TdkgAdapter(null);
        mAdapter.bindToRecyclerView(mRView);

        mSwitchType = getArguments().getString("type");
        mLampList = getArguments().getParcelableArrayList("list");

        mPresenter.initSwitchNum(mSwitchType);
    }

    @Override
    protected void initEvents(Dialog dialog) {
        mAdapter.setClickListener(new OnItemClick<SwitchBean>() {
            @Override
            public void onClick(SwitchBean info, int tag) {
                mType = info.isCheck() ? "1" : "0";
            }
        });
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                mPresenter.openClose(mLampList, mType);
                break;
        }
    }

    @Override
    public void fillSwitch(List<SwitchBean> dataList) {
        mAdapter.replaceData(dataList);
    }

    @Override
    public void onSuccess() {
        dismiss();
    }

}
